﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HanGu.Services.Interface;
using HanGu.Entity;

namespace HanGu.Services
{
    public class TravelStudyService : BaseService, ITravelStudy
    {

        public List<TravelStudy> GetAll()
        {
            using (HaiGuEntities obj = new HaiGuEntities())
            {
                return obj.TravelStudy.OrderBy(c=>c.Order).ThenByDescending(c => c.UpdateTime).ToList();
            }
        }

        public List<TravelStudy> GetAll(UniversallySearchParam searchparma)
        {
            using (HaiGuEntities obj = new HaiGuEntities())
            {
                var query = obj.TravelStudy.Where(c => 1 == 1);
                if (!string.IsNullOrEmpty(searchparma.Ttile))
                {
                    query = query.Where(c => c.Title.IndexOf(searchparma.Ttile) >= 0);
                }
                if (searchparma.StarDateTime.HasValue)
                {
                    query = query.Where(c => c.UpdateTime >= searchparma.StarDateTime);
                }
                if (searchparma.EndDateTime.HasValue)
                {
                    query = query.Where(c => c.UpdateTime <= searchparma.EndDateTime);
                }
                query = query.Where(c => c.ClassType == searchparma.ClassType);
                query = base.Paging<TravelStudy, DateTime?>(c => c.Order, c => c.UpdateTime, query, searchparma.Pagination);

                return query.ToList();
            }
        }

        public TravelStudy GetByID(int id)
        {
            using (HaiGuEntities obj = new HaiGuEntities())
            {
                return obj.TravelStudy.Where(c => c.ID == id).FirstOrDefault();
            }
        }
        public TravelStudy GetForIndex()
        {
            using (HaiGuEntities obj = new HaiGuEntities())
            {
                return obj.TravelStudy.Where(c => c.IsIndex == true).OrderBy(c=>c.Order).ThenByDescending(c => c.UpdateTime).FirstOrDefault();
            }
        }
        public List<TravelStudy> GetForTop(UniversallySearchParam searchparma)
        {
            using (HaiGuEntities obj = new HaiGuEntities())
            {
                var query = obj.TravelStudy.Where(c => c.IsTop == true);
                if (!string.IsNullOrEmpty(searchparma.Ttile))
                {
                    query = query.Where(c => c.Title == searchparma.Ttile);
                }
                if (searchparma.StarDateTime.HasValue)
                {
                    query = query.Where(c => c.UpdateTime >= searchparma.StarDateTime);
                }
                if (searchparma.EndDateTime.HasValue)
                {
                    query = query.Where(c => c.UpdateTime <= searchparma.EndDateTime);
                }
                query = query.Where(c=>c.ClassType==searchparma.ClassType);
                query = base.Paging<TravelStudy, DateTime?>(c => c.Order, c => c.UpdateTime, query, searchparma.Pagination);

                return query.ToList();
            }
        }

        public bool Add(TravelStudy model)
        {
            try
            {
                this.Add<TravelStudy>(model);
                return true;
            }
            catch { return false; }
        }

        public bool Update(TravelStudy model)
        {
            try
            {
                this.Update<TravelStudy>(model);
                return true;
            }
            catch { return false; }
        }

        public bool Delete(int id)
        {
            try
            {
                this.Delete<TravelStudy>(c => c.ID == id);
                return true;
            }
            catch { return false; }
        }


    }
}
